import { createRouter, createWebHistory } from 'vue-router'
import HomeView from '../views/HomeView.vue'
import OnmyojiView from '../views/OnmyojiView.vue'
import LoginView from '../views/LoginView.vue'
import RechargeView from '../views/RechargeView.vue'
import GameCenterView from '../views/GameCenterView.vue'

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/',
      name: 'home',
      redirect: '/onmyoji'
    },
    {
      path: '/onmyoji',
      name: 'onmyoji',
      component: OnmyojiView,
    },

    {
      path: '/login',
      name: 'login',
      component: LoginView,
    },
    

    {
      path: '/game-center',
      name: 'game-center',
      component: GameCenterView,
      // 添加路由守卫，需要登录才能访问
      meta: {
        requiresAuth: true
      }
    },
    {
      path: '/recharge',
      name: 'recharge',
      component: RechargeView,
      // 添加路由守卫，需要登录才能访问
      meta: {
        requiresAuth: true
      }
    },
  ],
})

// 添加路由守卫
router.beforeEach((to, from, next) => {
  // 检查路由是否需要身份验证
  if (to.meta.requiresAuth) {
    // 检查用户是否已登录（这里使用localStorage模拟）
    const currentUser = localStorage.getItem('currentUser')
    if (currentUser) {
      // 用户已登录，允许访问
      next()
    } else {
      // 用户未登录，重定向到登录页面
      next('/login')
    }
  } else {
    // 不需要身份验证的路由，直接放行
    next()
  }
})

export default router
